package com.totem.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.totem.base.enums.HandleStateEnum;
import com.totem.base.enums.JournalStateEnum;
import com.totem.base.enums.JournalTypeEnum;
import com.totem.customer.mapper.TInviteMapper;
import com.totem.customer.mapper.TMemberMapper;
import com.totem.customer.model.TCustomers;
import com.totem.customer.model.TInvite;
import com.totem.customer.model.TMembers;
import com.totem.customer.serivce.IMemberCommonService;
import com.totem.order.mapper.TOrderMapper;
import com.totem.order.mapper.TSubscribeOrderItemMapper;
import com.totem.order.mapper.TWalletJournalMapper;
import com.totem.order.model.TOrders;
import com.totem.order.model.TSubscribeOrderItems;
import com.totem.order.model.TWalletJournal;
import com.totem.product.mapper.TSubscribeMapper;
import com.totem.product.model.TSubscribe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Component
public class DistributiveAwardTask {
    @Autowired
    private IMemberCommonService iMemberCommonService;
    @Autowired
    private TOrderMapper tOrderMapper;
    @Autowired
    private TInviteMapper tInviteMapper;
    @Autowired
    private TMemberMapper tMemberMapper;
    @Autowired
    private TWalletJournalMapper tWalletJournalMapper;
    @Autowired
    private TSubscribeMapper tSubscribeMapper;
    @Autowired
    private TSubscribeOrderItemMapper tSubscribeOrderItemMapper;

    // 计算分销,已过时,不再调用
    @Deprecated
    public void distributiveAward(){
        List<TSubscribeOrderItems> unawardList = tSubscribeOrderItemMapper.findUnaward();
        if(CollectionUtils.isEmpty(unawardList)){
            return;
        }

        List<Long> orderIdList = unawardList.stream().map(TSubscribeOrderItems::getOrderId).toList();

        LambdaUpdateWrapper<TSubscribeOrderItems> updateItemWrapper = new LambdaUpdateWrapper<>();
        updateItemWrapper.set(TSubscribeOrderItems::getAwardHandleState, HandleStateEnum.HANDLING.getCode());
        updateItemWrapper.in(TSubscribeOrderItems::getOrderId, orderIdList);
        updateItemWrapper.eq(TSubscribeOrderItems::getAwardHandleState, HandleStateEnum.UNHANDLED.getCode());
        int effect = tSubscribeOrderItemMapper.update(updateItemWrapper);
        if(effect<=0){
            return;
        }

        LambdaQueryWrapper<TSubscribeOrderItems> queryHanding = new LambdaQueryWrapper<>();
        queryHanding.eq(TSubscribeOrderItems::getAwardHandleState, HandleStateEnum.HANDLING.getCode());
        List<TSubscribeOrderItems> handlingList = tSubscribeOrderItemMapper.selectList(queryHanding);
        if(CollectionUtils.isEmpty(handlingList)){
            log.warn("没有在处理中的待奖励的认购单");
            return;
        }
        List<Long> handlingIdList = handlingList.stream().map(TSubscribeOrderItems::getOrderId).toList();
        List<TOrders> orderList = tOrderMapper.selectBatchIds(handlingIdList);
        List<TSubscribe> subscribeList = tSubscribeMapper.selectList(new QueryWrapper<>());

        LambdaQueryWrapper<TWalletJournal> journalListQuery = new LambdaQueryWrapper<>();
        journalListQuery.in(TWalletJournal::getOrderId, handlingIdList);
        journalListQuery.eq(TWalletJournal::getJournalType, JournalTypeEnum.SUBSCRIBE_ORDER.getCode());
        journalListQuery.eq(TWalletJournal::getJournalState, JournalStateEnum.FINISHED.getCode());
        List<TWalletJournal> journalList = tWalletJournalMapper.selectList(journalListQuery);
        if(CollectionUtils.isEmpty(journalList)){
            log.info("无新增业绩流水");
            return;
        }

        Map<Long, TSubscribeOrderItems> unawardMap = unawardList.stream().collect(Collectors.toMap(TSubscribeOrderItems::getOrderId, Function.identity()));
        Map<String, TSubscribe> subscribeMap = subscribeList.stream().collect(Collectors.toMap(TSubscribe::getSubscribeName, Function.identity()));
        Map<Long, TWalletJournal> journalMap = journalList.stream().collect(Collectors.toMap(TWalletJournal::getOrderId, Function.identity()));
        orderList.forEach(order->{
            Long orderId = order.getId();
            TSubscribeOrderItems subscribeItem = unawardMap.get(orderId);
            TWalletJournal journal = journalMap.get(orderId);
            TSubscribe tSubscribe = subscribeMap.get(subscribeItem.getSubscribeName());
            iMemberCommonService.calDistributiveAward(order, journal, tSubscribe);

            TSubscribeOrderItems updateItem = TSubscribeOrderItems.builder()
                    .id(subscribeItem.getId())
                    .awardHandleState(HandleStateEnum.HANDLED.getCode())
                    .build();
            tSubscribeOrderItemMapper.updateById(updateItem);
        });
    }
}
